package com.android.view.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.view.activity.base.Constants;
import com.android.view.fragment.info.ExchangeFragment;
import com.android.view.fragment.info.MainFragment;
import com.android.view.fragment.info.MargeTradeFragment;
import com.android.view.fragment.info.MarketsFragment;
import com.android.view.fragment.info.UserFragment;
import com.android.view.widget.NoScrollViewPager;
import com.android.R;
import com.android.view.activity.base.ToolbarBaseActivity;
import com.android.view.adapter.base.BaseFragmentPageAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;

/**
 * author:  TenFace
 * date:    2018/3/23
 * description: KLineChart
 */

public class GalaxyMainActivity extends ToolbarBaseActivity {
    private static final String TAG = GalaxyMainActivity.class.getSimpleName();
    @BindView(R.id.activity_main_no_scroll_view_pager)
    NoScrollViewPager noScrollViewPager;
    @BindViews({R.id.activity_main_ll_main, R.id.activity_main_ll_quote, R.id.activity_main_ll_exchange, R.id.activity_main_ll_marge_trade, R.id.activity_main_ll_personal})
    RelativeLayout[] llTabs;
    @BindViews({R.id.activity_main_iv_main, R.id.activity_main_iv_quote, R.id.activity_main_iv_exchange, R.id.activity_main_iv_marge_trade, R.id.activity_main_iv_personal})
    ImageView[] ivTabs;
    @BindViews({R.id.activity_main_tv_main, R.id.activity_main_tv_quote, R.id.activity_main_tv_exchange, R.id.activity_main_tv_marge_trade, R.id.activity_main_tv_personal})
    TextView[] tvTabs;
    //    String tabName[] = {getString(R.string.main_title),getString(R.string.markets_title),getString(R.string.feature_title),getString(R.string.balances_title),getString(R.string.account_title)};
//    String tabName[];
    String tabName[] = {"Home", "Markets", "SICO", "Balances", "Account"};
    int normalIv[] = {R.mipmap.main_home_checked, R.mipmap.main_markets_checked, R.mipmap.main_sico_checked, R.mipmap.main_balances_checked, R.mipmap.main_account_checked};
    int focusIv[] = {R.mipmap.main_home_normal, R.mipmap.main_markets_normal, R.mipmap.main_sico_normal, R.mipmap.main_balances_normal, R.mipmap.main_account_normal};

    List<Fragment> listFragment;
    BaseFragmentPageAdapter fragmentAdapter;
    int currentIndex = 0;
    int clickTabIndex = 0;

    @Override
    protected void setContentLayout() {
        setContentView(R.layout.activity_galaxy_main);
    }

    @Override
    protected void initView() {
        getToolbar().setVisibility(View.GONE);
        initViewPager();
    }

    @Override
    protected void obtainData() {
//        EventBusUtil.register(this);//订阅事件
    }

    @Override
    protected void initEvent() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Constants.activityList.put(TAG, this);
    }

    private void initViewPager() {
        listFragment = new ArrayList<>();
        listFragment.add(MainFragment.newInstance(1));
        listFragment.add(MarketsFragment.newInstance(1));
        listFragment.add(ExchangeFragment.newInstance(1));
        listFragment.add(MargeTradeFragment.newInstance(1));
        listFragment.add(UserFragment.newInstance(1));

        fragmentAdapter = new BaseFragmentPageAdapter(getSupportFragmentManager(), listFragment);
        noScrollViewPager.setAdapter(fragmentAdapter);
        noScrollViewPager.addOnPageChangeListener(new MyPageChangeListener());
        noScrollViewPager.setOffscreenPageLimit(5);
//        tabName[0] = getString(R.string.main_title);
//        tabName[1] = getString(R.string.markets_title);
//        tabName[2] = getString(R.string.feature_title);
//        tabName[3] = getString(R.string.balances_title);
//        tabName[4] = getString(R.string.account_title);
        for (int i = 0; i < tabName.length; i++) {
            tvTabs[i].setText(tabName[i]);
            llTabs[i].setOnClickListener(new TabClickListener(i));
        }
        tabFocus(currentIndex);
    }


    private class MyPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            tabFocus(position);
            currentIndex = position;
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    }

    private class TabClickListener implements View.OnClickListener {
        int index;

        private TabClickListener(int index) {
            this.index = index;
        }

        @Override
        public void onClick(View v) {
            clickTabIndex = index;
            switch (clickTabIndex) {
                case TAB1:
                    noScrollViewPager.setCurrentItem(clickTabIndex);
                    break;
                case TAB2:
                    noScrollViewPager.setCurrentItem(clickTabIndex);
                    break;
                case TAB3:
                    noScrollViewPager.setCurrentItem(clickTabIndex);
                    break;
                case TAB4:
                    noScrollViewPager.setCurrentItem(clickTabIndex);
                    break;
                case TAB5:
                    noScrollViewPager.setCurrentItem(clickTabIndex);
                    break;
            }

        }
    }

    private void tabFocus(int position) {
        for (int i = 0; i < tabName.length; i++) {
            if (i == position) {
                //聚焦状态
                ivTabs[i].setImageResource(focusIv[i]);
                tvTabs[i].setTextColor(getResources().getColor(R.color.main_white));
            } else {
                //非聚焦tab
                ivTabs[i].setImageResource(normalIv[i]);
                tvTabs[i].setTextColor(getResources().getColor(R.color.main_button));
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //取消订阅
//        EventBusUtil.unregister(this);
    }


}
